﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BuXiaChu.OrderDishes.Model;
using BuXiaChu.OrderDishes.BLL;
using System.Web.Script.Serialization;
using System.Text;

namespace BuXiaChu.OrderDishes.Web.CallCenter.Modules
{
    public partial class DishesList : System.Web.UI.UserControl
    {
        protected List<DishesModel> pageDataList = null;
        protected List<DishesCatModel> dishesCatList = null;
        public string RestaurantName
        {
            get;
            set;
        }

        public int RestaurantId
        {
            get;
            set;
        }

        public string SerializeData(DishesModel obj)
        {
            #region
            var jsonObj = new
            {
                restaurantName = RestaurantName.Trim(),
                restaurantId = RestaurantId,
                id = obj.CodeId,
                name = obj.Name.Trim(),
                catName = GetDishesCatName(obj.CatID),
                price = obj.Price.ToString("F2"),
                discount = obj.Discount,
                invoiceDiscount = obj.InvoiceDiscount,
                unitName = CodeDictionary.Instance.GetCodeName(obj.Unit),
                unit = obj.Unit,
                number = 1,
                boxNum = obj.Box,
                boxPrice = obj.BoxUnitPrice
            };
            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            StringBuilder jsonText = new StringBuilder();
            jsSerializer.Serialize(jsonObj, jsonText);
            return jsonText.ToString();
            #endregion
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            var sqlWhere = " RestaurantId = " + RestaurantId;
            var pageCout = 0;
            pageDataList = RestaurantManager.SearchDishes(sqlWhere, 0, 0, out pageCout);

            if (pageDataList.Count > 0)
            {
                dishesCatList = RestaurantManager.GetDishesCatByRestaurantId(pageDataList[0].RestaurantId);
            }
            else
            {
                dishesCatList = new List<DishesCatModel>();
            }
        }

        protected string GetDishesCatName(int catId)
        {
            var result = "";
            foreach (var item in dishesCatList)
            {
                if (item.CatID == catId)
                {
                    result = item.Name;
                    break;
                }
            }
            return result;
        }
    }
}